Add test for local gitconf
authorHunter Praska <hunter@wiggin-labs.com>
Wed, 7 Jun 2017 00:24:00 +0000 (19:24 -0500)
committerHunter Praska <hunter@wiggin-labs.com>
Wed, 7 Jun 2017 00:24:00 +0000 (19:24 -0500)
src/cargo/ops/cargo_new.rs
tests/new.rs

index 1e92aa5c785673688ea3dd41b189726d6c8ce766..53bc1a431ef4d5b10b6c410b253ad5757de34a74 100644 (file)
@@ -517,7 +517,7 @@ fn get_environment_variable(variables: &[&str] ) -> Option<String>{
 fn discover_author() -> CargoResult<(String, Option<String>)> {
     let cwd = env::current_dir()?;
     let git_config = if let Ok(repo) = GitRepository::discover(&cwd) {
-        repo.config().ok()
+        repo.config().ok().or_else(|| GitConfig::open_default().ok())
     } else {
         GitConfig::open_default().ok()
     };
index 0312d779534e86a57d326f99c635dfbb0f0ad9de..bceda09377ff623f9b921468489d16dd027e9851 100644 (file)
@@ -281,6 +281,29 @@ fn finds_author_git() {
     assert!(contents.contains(r#"authors = ["bar <baz>"]"#));
 }
 
+#[test]
+fn finds_local_author_git() {
+    process("git").args(&["init"])
+        .exec().unwrap();
+    process("git").args(&["config", "--global", "user.name", "foo"])
+                  .exec().unwrap();
+    process("git").args(&["config", "--global", "user.email", "foo@bar"])
+                  .exec().unwrap();
+
+    // Set local git user config
+    process("git").args(&["config", "user.name", "bar"])
+                  .exec().unwrap();
+    process("git").args(&["config", "user.email", "baz"])
+                  .exec().unwrap();
+    assert_that(cargo_process("init").env("USER", "foo"),
+                execs().with_status(0));
+
+    let toml = paths::root().join("Cargo.toml");
+    let mut contents = String::new();
+    File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
+    assert!(contents.contains(r#"authors = ["bar <baz>"]"#));
+}
+
 #[test]
 fn finds_git_email() {
     let td = TempDir::new("cargo").unwrap();